home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2949 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  62 lines

  1. Path: castle.nando.net!news
  2. From: actuary@nando.net   (Bill McCarthy)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: help with pi algorithm
  5. Date: 25 Jan 1996 05:29:41 GMT
  6. Organization: News & Observer Public Access
  7. Message-ID: <4e74g5$ot1@castle.nando.net>
  8. References: <0fc_9601240111@csource.blaze.net.au>
  9. Reply-To: actuary@nando.net (Bill McCarthy)
  10. NNTP-Posting-Host: 152.52.37.91
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <0fc_9601240111@csource.blaze.net.au>, Andrew.Nesbit@f396.n634.z3.fidonet.org (Andrew Nesbit) writes:
  14. >Hi all!
  15. >
  16. >I am a newbie C programmer trying to write an algorithm to work out the
  17. >value of pi fairly accurately. I have written the following code, but I'm
  18. >not sure whether it's considered 'nasty' programming, or whether it could
  19. >be written to run more quickly.
  20. >
  21. >#include <stdio.h>
  22. >main()
  23. >{
  24. >   long double pi = 0;
  25. >   long int = count;
  26. >   for (count = 1; count <= 300000; count += 4) {
  27. >      pi += 4.0 / count;            pi -= 4.0 / (count + 2);
  28. >   }
  29. >   printf("Value of pi is approx %.19Lf)", pi);
  30. >   return 0;
  31. >}
  32. >
  33. >Any comments please???
  34.  
  35. You appear to be using the famous Leibniz Series from the
  36. 17th century.  It works but is a bit slow.
  37.  
  38. Try the Tamura-Kanada Algorithm and watch this sucker
  39. converge:
  40.  
  41. double tamura( int n )
  42. {
  43.    double a = 1, b = 1 / sqrt(2), c = .25, p2 = 1, y;
  44.  
  45.    while( --n > 0 )
  46.    {
  47.       y = a;
  48.       a = (a + b) / 2;
  49.       b = sqrt(b * y);
  50.       y = a - y;
  51.       c -=  p2 * y * y;
  52.       p2 *= 2;
  53.    }
  54.  
  55.    b += a;
  56.    return b * b / c / 4;
  57. }
  58.  
  59. Bill McCarthy
  60. actuary@nando.net
  61. Wendell, NC  USA
  62.